package com.alan.learn.validator.controller;

import com.alan.learn.validator.entity.User;
import com.alan.learn.fluentValidator.LengthValidator;
import com.alan.learn.fluentValidator.NotNullValidator;
import com.alan.learn.fluentValidator.PhoneValidator;
import com.alan.learn.fluentValidator.SizeValidator;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liufan
 * 2019-10-14
 */
@Api(tags = "Validator 验证")
@RequestMapping(value = "/validator", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class ValidatorController {

    @ApiOperation(value = "提交建议反馈", notes = "")
    @PostMapping(value = "/validatorEntity.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object validatorEntity(@RequestBody User user) {
        //校验
        ComplexResult complexResult = FluentValidator.checkAll()
                .on(user.getName(),new NotNullValidator("客户名称"))
                .on(user.getName(), new LengthValidator(6, 10, "客户名称"))
                .on(user.getAge(),new SizeValidator(0,150,"年龄"))
                .on(user.getPhone(),new NotNullValidator("手机号"))
                .on(user.getPhone(),new PhoneValidator("手机号"))
                .doValidate()
                .result(ResultCollectors.toComplex());

        return  complexResult;
    }








}
